home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d15 / face.arc / FACE.C < prev    next >
Text File  |  1990-09-07  |  5KB  |  147 lines

  1. /***********************************************************************
  2.  * This program was originally written by Michael Harrison [76057,101] *
  3.  * The only problem with the original that I could see was that it was *
  4.  * somewhat of a memory hog.  The face bitmap that gets popped up on   *
  5.  * the screen was reloaded every time a face was supposed to pop up.   *
  6.  * Thus, the GDI ended up with about 100 copies of the bitmap (more if *
  7.  * the user let it run after it asked if that was enough).  Even though*
  8.  * this program is kinder to the victim in that it removes itself from *
  9.  * memory after the user terminates it, only the last bitmap was       *
  10.  * removed from the GDI resource pool.  I've cleaned this up a bit.    *
  11.  * Perri Nelson [71401,2116]                                           *
  12.  ***********************************************************************/
  13. /*** Include files needed for definitions, declarations, etc.         ***/
  14. #include <windows.h>  /* Required for all Windows programs              */
  15. #include <stdlib.h>
  16.  
  17. /*** Forward declare functions                                        ***/
  18. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  19.  
  20. HANDLE    hInst;
  21.  
  22.     /* This was made global to this source file so that WinMain and
  23.        WinProc could both access it.  This way, the bitmap doesn't
  24.        Get loaded more than once per instance. */
  25.  
  26.     static HANDLE hBitmap;
  27.  
  28. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  29.     HANDLE      hInstance, hPrevInstance;
  30.     LPSTR       lpszCmdLine;
  31.     int         nCmdShow;
  32.     {
  33.     HWND        hWnd;
  34.     MSG         msg;
  35.     WNDCLASS    wndclass;
  36.  
  37.     if (!hPrevInstance)
  38.         {
  39.         wndclass.style          = CS_HREDRAW;
  40.         wndclass.lpfnWndProc    = WndProc;
  41.         wndclass.cbClsExtra     = 0;
  42.         wndclass.cbWndExtra     = 0;
  43.         wndclass.hInstance      = hInstance;
  44.         wndclass.hIcon          = NULL;
  45.         wndclass.hCursor        = LoadCursor (hInstance, IDC_ARROW);
  46.         wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH);
  47.         wndclass.lpszMenuName   = NULL;
  48.         wndclass.lpszClassName  = "Face";
  49.  
  50.     if (!RegisterClass (&wndclass) )
  51.         return FALSE;
  52.         }
  53.     else
  54.         return FALSE;
  55.  
  56.     hInst=hInstance;
  57.     hWnd = CreateWindow ("Face", "Face", WS_OVERLAPPEDWINDOW, 0, 0,
  58.                         0,0, NULL, NULL, hInstance, NULL);
  59.  
  60.  
  61.     if(!SetTimer(hWnd, 1, 100, NULL))
  62.         {
  63.         MessageBox(hWnd, "Too many clocks or timers!", "Error",
  64.                    MB_ICONEXCLAMATION | MB_OK);
  65.         return FALSE;
  66.         }
  67.  
  68.                 /* I moved this out of WndProc, to keep it from
  69.                    being loaded more than once for each instance */
  70.  
  71.                 hBitmap=LoadBitmap(hInst, "Face");
  72.  
  73.     while (GetMessage(&msg, NULL, 0,0 ))
  74.         {
  75.         TranslateMessage(&msg);
  76.         DispatchMessage(&msg);
  77.         }
  78.     return msg.wParam;
  79.     }
  80.  
  81. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  82.     HWND          hWnd;
  83.     unsigned      iMessage;
  84.     WORD          wParam;
  85.     LONG          lParam;
  86.     {
  87.     BITMAP        bm;
  88.     HDC           hDC,hMemDC;
  89.     short         nRand, nIndex;
  90.     long          xStart, yStart;
  91.     static char   cJunk=0;
  92.  
  93.  
  94.  
  95.     switch (iMessage)
  96.         {
  97.         case WM_TIMER:
  98.             if(rand() < 1500)
  99.                 {
  100.  
  101.                 GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &bm);
  102.  
  103.                 hDC=CreateDC("DISPLAY", NULL, NULL, NULL);
  104.                 hMemDC=CreateCompatibleDC(hDC);
  105.                 SelectObject(hMemDC, hBitmap);
  106.  
  107.                 xStart=rand() % GetSystemMetrics(SM_CXSCREEN);
  108.                 yStart=rand() % GetSystemMetrics(SM_CYSCREEN);
  109.                 BitBlt(hDC, (short)xStart, (short)yStart, bm.bmWidth, bm.bmHeight,
  110.                        hMemDC, 0,0, SRCAND);
  111.  
  112.                 DeleteDC(hDC);
  113.                 DeleteDC(hMemDC);
  114.  
  115.                 if ( cJunk == 100 ) {
  116.                     cJunk = 0;
  117.                     KillTimer( hWnd, 1 );
  118.                     if ( MessageBox( hWnd, "Had enough?", "Face by MSH",MB_YESNO | MB_ICONQUESTION ) == IDYES )
  119.                         SendMessage( hWnd, WM_CLOSE, 0, 0L );
  120.                     else
  121.                         if(!SetTimer(hWnd, 1, 100, NULL))
  122.                         {
  123.                             MessageBox(hWnd, "Too many clocks or timers!", "Error",
  124.                             MB_ICONEXCLAMATION | MB_OK);
  125.                         }
  126.                 }
  127.                 else
  128.                     cJunk++;
  129.                 }
  130.             break;
  131.  
  132.         case WM_CREATE:
  133.             srand((unsigned)GetTickCount());
  134.             break;
  135.  
  136.         case WM_DESTROY:
  137.             DeleteObject(hBitmap);
  138.             PostQuitMessage(0);
  139.             break;
  140.  
  141.  
  142.         default:
  143.             return (DefWindowProc(hWnd, iMessage, wParam, lParam));
  144.         }
  145.     return 0L;
  146. }
  147.